# -*- coding: utf-8 -*-
from time import sleep

import allure
from common.page import Page
from regression_service_rdc.funtion.fun_main_rdc import MainRdc
from regression_service_rdc.page_elements.el_branch_management_rdc import branch_rdc


class BranchManagementListRdc(MainRdc):
    @allure.step('step:申请分支')
    def click_create_branch(self):
        Page.click(branch_rdc,'分支管理列表','申请分支')
        return CreateBranchPage()

    @allure.step('step:在分支搜索框输入分支名称搜索')
    def sendkeys_lookup_branch_name(self,branch_name):
        Page.send_key(branch_rdc,'分支管理列表','搜索分支名称',branch_name)
        sleep(1)
        return self

    @allure.step('step:搜索分支名称后获取到的第一个分支名称')
    def __lookup_branch_name_text(self):
        actual_branch_name = Page.element_text(branch_rdc,'分支管理列表','获取分支名称')
        return actual_branch_name

    @allure.step('step:断言实际创建分支名称与预期分支名称一致')
    def assert_branch_name_equal(self,expect_branch_name):
        sleep(1)
        self.sendkeys_lookup_branch_name(expect_branch_name)
        sleep(1)
        actual_branch_name = self.__lookup_branch_name_text()
        Page.assert_equal(expect_branch_name,actual_branch_name)
        return self

    @allure.step('step:断言查找到的分支状态为【开发中】')
    def assert_branch_state(self,expect_state):
        # 等待应用状态变为[失败]或者[开发中]
        branch_state_text = Page.wait_not_text(branch_rdc, '分支管理列表', '获取分支状态')
        Page.assert_equal(branch_state_text,expect_state)
        return self

    @allure.step('step:鼠标悬停到更多分支信息上')
    def move_to_more_information(self):
        Page.move_to_element(branch_rdc,'分支管理列表','更多分支信息')
        return self

    @allure.step('step:删除搜索到的分支')
    def delete_lookup_branch_name(self,branch_name):
        Page.click(branch_rdc,'分支管理列表','分支删除按钮')
        sleep(0.5)
        Page.send_key(branch_rdc,'确认分支删除页面','输入特性分支名称',branch_name)
        sleep(0.2)
        Page.click(branch_rdc,'确认分支删除页面','删除')
        sleep(2)
        return self

    @allure.step('step:断言删除成功')
    def assert_delete_lookup_branch_name(self):
        el_none = Page.element(branch_rdc,'分支管理列表', '分支删除按钮')
        Page.assert_equal(None,el_none)
        return self

class CreateBranchPage(MainRdc):
    @allure.step('step:输入分支名称')
    def sendkeys_branch_name(self,branch_name):
        Page.send_key(branch_rdc,'申请分支页面','输入分支名称',branch_name)
        return self

    @allure.step('step:产品_选择关联任务')
    def select_product_associated_task(self):
        Page.click(branch_rdc,'申请分支页面','点击关联任务')
        sleep(1)
        Page.click(branch_rdc,'申请分支页面','产品_选择关联任务2')
        return self

    @allure.step('step:项目_选择关联任务')
    def select_project_associated_task(self):
        Page.click(branch_rdc,'申请分支页面','点击关联任务')
        sleep(1)
        Page.click(branch_rdc,'申请分支页面','项目_选择关联任务2')
        return self

    @allure.step('step:点击创建默认环境')
    def click_create_default_environment(self):
        Page.click(branch_rdc,'申请分支页面','点击创建默认环境')
        return self

    @allure.step('step:输入测试库名称')
    def sendkeys_test_database_name(self,test_database_name):
        Page.send_key(branch_rdc,'申请分支页面','输入测试库名称',test_database_name)
        return self

    @allure.step('step:点击保存')
    def click_preserve(self):
        Page.click(branch_rdc,'申请分支页面','保存')
        return BranchManagementListRdc()
